'use strict';
module.exports = {
	/**
	 * XXXnameXXX
	 * @url agent/setting/kh/RebackPercent 前端调用的url参数地址
	 * data 请求参数
	 * @param {String} params1  参数1
	 */
	main: async (event) => {
		const db = uniCloud.database();
		let {
			data = {}, userInfo, util, filterResponse, originalParam
		} = event;
		let {
			customUtil,
			uniID,
			config,
			pubFun,
			vk,
			_
		} = util;
		let {
			uid,
			type,
			nid,
			huichuan,
			amountRanges,
		} = data;
		let res = {
			code: 0,
			msg: ""
		};
		// 业务逻辑开始-----------------------------------------------------------

		let huichuans = await db.collection('a_klmb').where({
			_id: nid
		}).get()

		console.log("huichuans", huichuans);
		if (huichuans.data.length > 0) {
			console.log("进入修改", nid);
			const update_order = await db.collection('a_klmb').where({
				_id: nid
			}).update({
				name: "RebackPercent",
				huichuana: huichuan,
				koulianbz: huichuan.koulianbz,
				koulianmc: huichuan.koulianmc,
				amountRanges: amountRanges,
				middleman_id: userInfo.middleman_id,
				middleman_nickname: userInfo.middleman_nickname,
				middleman_username: userInfo.middleman_username,
				username: userInfo.username,
				nickname: userInfo.nickname,
				_add_time: new Date().getTime(),
				update_date: vk.pubfn.timeFormat(new Date(), "yyyy-MM-dd hh:mm:ss")
			})

			//模板修改之后 同步所有链接得绑定得扣量

			await vk.baseDao.update({
				dbName: "a_config", // 表名
				whereJson: { // 条件
					mid: nid,
				},
				dataJson: { // 需要修改的数据

					koulianbz: huichuan.koulianbz,
					koulianmc: huichuan.koulianmc,
					lqdhcbs: huichuan.lqdhcbs,
					newuser: huichuan.newuser,
					amountRanges: amountRanges,
					douyinRadio: huichuan.douyinRadio,
					firstorder: huichuan.firstorder,
					hour_backhaul: huichuan.hour_backhaul,
					update_date: vk.pubfn.timeFormat(new Date(), "yyyy-MM-dd hh:mm:ss")
				}
			});

		} else {

			let adds = await db.collection('a_klmb').add({
				name: "RebackPercent",
				huichuana: huichuan,
				koulianbz: huichuan.koulianbz,
				koulianmc: huichuan.koulianmc,
				amountRanges: amountRanges,
				middleman_id: userInfo.middleman_id,
				middleman_nickname: userInfo.middleman_nickname,
				middleman_username: userInfo.middleman_username,
				username: userInfo.username,
				nickname: userInfo.nickname,
				_add_time: new Date().getTime(),
				_add_time_str: vk.pubfn.timeFormat(new Date(), "yyyy-MM-dd hh:mm:ss")


			})

		}






		// 业务逻辑结束-----------------------------------------------------------
		return res;
	}
}